﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Daneas.Utility.Universals
{
    public class HtmlHelper
    {
        #region 去掉HTML中的所有标签,只留下纯文本
        /// <summary>
        /// 去掉HTML中的所有标签,只留下纯文本
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string CleanHtml1(string strHtml)
        {
            if (string.IsNullOrEmpty(strHtml)) return strHtml;
            //删除脚本
            //Regex.Replace(strHtml, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase)
            strHtml = Regex.Replace(strHtml, "(<script(.+?)</script>)|(<style(.+?)</style>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            //删除标签
            var r = new Regex(@"</?[^>]*>", RegexOptions.IgnoreCase);
            Match m;
            for (m = r.Match(strHtml); m.Success; m = m.NextMatch())
            {
                strHtml = strHtml.Replace(m.Groups[0].ToString(), "");
            }
            return strHtml.Trim();
        }


        public static string CleanHtml2(string strHtml)
        {
            if (strHtml == null)
            {
                throw new Exception("Your input html stream is null!");
            }
            /*
             * 最好把所有的特殊HTML标记都找出来，然后把与其相对应的Unicode字符一起影射到Hash表内，最后一起都替换掉
             */
            //先单独测试,成功后,再把所有模式合并
            //注:这两个必须单独处理
            //去掉嵌套了HTML标记的JavaScript:(<script)[\\s\\S]*(</script>)
            //去掉css标记:(<style)[\\s\\S]*(</style>)
            //去掉css标记:\\..*\\{[\\s\\S]*\\}
            strHtml = Regex.Replace(strHtml, "(<script)[\\s\\S]*?(</script>)|(<style)[\\s\\S]*?(</style>)", " ", RegexOptions.IgnoreCase);
            //htmlStream = RemoveTag(htmlStream, "script");
            //htmlStream = RemoveTag(htmlStream, "style");
            //去掉普通HTML标记:<[^>]+>
            //替换空格:&nbsp;|&amp;|&shy;|&#160;|&#173;
            strHtml = Regex.Replace(strHtml, "<[^>]+>|&nbsp;|&amp;|&shy;|&#160;|&#173;|&bull;|&lt;|&gt;", " ", RegexOptions.IgnoreCase);
            //htmlStream = RemoveTag(htmlStream);
            //替换左尖括号
            //htmlStream = Regex.Replace(htmlStream, "&lt;", "<");
            //替换右尖括号
            //htmlStream = Regex.Replace(htmlStream, "&gt;", ">");
            //替换空行
            //htmlStream = Regex.Replace(htmlStream, "[\n|\r|\t]", " ");//[\n|\r][\t*| *]*[\n|\r]
            strHtml = Regex.Replace(strHtml, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n");
            strHtml = Regex.Replace(strHtml, "[\t| ]{1,}", " ");
            return strHtml.Trim();
        }
        #endregion

        #region 去除style
        public static string CleanStyle(string strHtml)
        {
            strHtml = Regex.Replace(strHtml, "style=\"(?<d>[^>]*)", "");
            strHtml = Regex.Replace(strHtml, "STYLE=\"(?<d>[^>]*)", "");
            strHtml = strHtml.Replace("<span>", "");
            strHtml = strHtml.Replace("<SPAN>", "");
            strHtml = strHtml.Replace("</span>", "");
            strHtml = strHtml.Replace("</SPAN>", "");
            strHtml = strHtml.Replace("<o:p></o:p>", "");
            return strHtml;
        }
        #endregion

        /// <summary>
        /// 异步加载分页控件
        /// </summary>
        /// <param name="nextpage">下一页</param>
        /// <param name="totalpage">总页数</param>
        /// <param name="function">js功能名和参数</param>
        /// <param name="showwords"></param>
        /// <returns></returns>
        public static string GetLoadPage(int nextpage, int totalpage, string data, string showwords)
        {
            string html = "<!--目前的页面，总页数，自定义方法字符串，加载显示内容-->";
            html += "<div class=\"loadpage\">";
            html += "<input id =\"nextpage\" value=\"{0}\" type=\"hidden\" />";
            html += "<input id =\"totalpage\" value=\"{1}\"  type=\"hidden\" />";
            html += "<a href=\"javascript:loadcontent({2})\">{3}</a>";
            html += "</div>";

            return string.Format(html, nextpage, totalpage, data, showwords);
        }
    }
}
